package readini

import "fmt"

// Section 存储该分区内所有键值对 key-value.
type Section struct {
	// 存储 key.
	KeyList []string
	// key 和 value 之间的映射.
	KeyVal map[string]string
}

// newKeyVal 创建新键值对 key-value.
func (sec *Section) newKeyVal(keyName string, value string) error {
	if _, ok := sec.KeyVal[keyName]; ok {
		return fmt.Errorf("key(%v) already exists", keyName)
	}
	sec.KeyList = append(sec.KeyList, keyName)
	sec.KeyVal[keyName] = value
	return nil
}

// GetKey  根据 key 返回对应的 value.
func (sec *Section) GetKey(keyName string) (string, error) {
	if _, ok := sec.KeyVal[keyName]; !ok {
		return "", fmt.Errorf("key(%v) does not exist", keyName)
	}
	return sec.KeyVal[keyName], nil
}

// GetKeyList 返回该分区中所有 key 组成的列表.
func (sec *Section) GetKeyList() []string {
	keyList := make([]string, len(sec.KeyList))
	for i := range sec.KeyList {
		keyList[i] = sec.KeyList[i]
	}
	return keyList
}
